package com.sskj.one.transaction.adapter;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sskj.common.base.App;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.one.R;
import com.sskj.one.bean.PositionBean;
import com.sskj.one.util.ArithUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PositionAdapter extends RecyclerView.Adapter<PositionAdapter.ViewHolder> {
    private List<PositionBean> mDatas;
    //用于退出activity,避免countdown，造成资源浪费。
    private SparseArray<CountDownTimer> countDownMap;

    public PositionAdapter(Context context, List<PositionBean> mDatas) {
        this.mDatas = mDatas;
        countDownMap = new SparseArray<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_position, parent, false);
        return new ViewHolder(view);
    }

    /**
     * 清空资源
     */
    public void cancelAllTimers() {
        if (countDownMap == null) {
            return;
        }
        for (int i = 0, length = countDownMap.size(); i < length; i++) {
            CountDownTimer cdt = countDownMap.get(countDownMap.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final PositionBean data = mDatas.get(position);
        holder.tv_status.setText("1".equals(data.getOtype()) ? "买涨" : "买跌");
        holder.tv_status.setTextColor("1".equals(data.getOtype()) ?
                ContextCompat.getColor(App.INSTANCE, R.color.color_red) : ContextCompat.getColor(App.INSTANCE, R.color.theme));
        holder.tv_look_time.setText(TimeFormatUtil.formatE(Long.valueOf(data.getAddtime()) * 1000));
        holder.tv_new_rate.setText(NumberUtil.keep(data.getNowPrice(), 5));
        holder.tv_earn.setText(Float.valueOf(data.getNowPrice()) < Float.valueOf(data.getBuyprice()) ? "$ 0.00000"
                : "$ " + NumberUtil.keep((float) ArithUtil.mul(Double.valueOf(data.getLeverage()), Double.valueOf(data.getDeposit())), 5));
        holder.tv_xia_rate.setText(data.getBuyprice());
        holder.tv_money.setText("$ " + NumberUtil.keep(data.getDeposit(), 5));
        //将前一个缓存清除
        if (holder.countDownTimer != null) {
            holder.countDownTimer.cancel();
        }
        if (Long.valueOf(data.getCycle_time()) > 0) {
            holder.countDownTimer = new CountDownTimer(Long.valueOf(data.getCycle_time()) * 1000, 1000) {
                public void onTick(long millisUntilFinished) {
                    Log.i("PositionAdapter", "onTick: " + millisUntilFinished);
                    holder.tv_count_down.setText(millisUntilFinished / 1000 + "s");
                }

                public void onFinish() {
                    mDatas.remove(data);
                    notifyDataSetChanged();
                    holder.tv_count_down.setText("0s");

                }
            }.start();
            countDownMap.put(holder.tv_count_down.hashCode(), holder.countDownTimer);
        } else {
            holder.tv_count_down.setText("0s");
        }
    }

    @Override
    public int getItemCount() {
        if (mDatas != null && !mDatas.isEmpty()) {
            return mDatas.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_status)
        TextView tv_status;
        @BindView(R.id.tv_look_time)
        TextView tv_look_time;
        @BindView(R.id.tv_new_rate)
        TextView tv_new_rate;
        @BindView(R.id.tv_earn)
        TextView tv_earn;
        @BindView(R.id.tv_xia_rate)
        TextView tv_xia_rate;
        @BindView(R.id.tv_money)
        TextView tv_money;
        @BindView(R.id.tv_count_down)
        TextView tv_count_down;
        public CountDownTimer countDownTimer;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
